package com.ruoyi.ltd_web.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ltd_web.domain.LtdContent;
import com.ruoyi.ltd_web.mapper.LtdContentMapper;
import com.ruoyi.ltd_web.service.ILtdContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


/**
 * 内容Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-04
 */
@Service
public class LtdContentServiceImpl implements ILtdContentService
{
    @Autowired
    private LtdContentMapper ltdContentMapper;
    @Autowired
    private RuoYiConfig ruoYiConfig;

    /**
     * 查询内容
     *
     * @param contentId 内容主键
     * @return 内容
     */
    @Override
    public LtdContent selectLtdContentByContentId(Long contentId)
    {
        return ltdContentMapper.selectLtdContentByContentId(contentId);
    }

    /**
     * 查询内容列表
     *
     * @param ltdContent 内容
     * @return 内容
     */
    @Override
    public List<LtdContent> selectLtdContentList(LtdContent ltdContent)
    {
        return ltdContentMapper.selectLtdContentList(ltdContent);
    }

    /**
     * 新增内容
     *
     * @param ltdContent 内容
     * @return 结果
     */
    @Override
    public int insertLtdContent(LtdContent ltdContent)
    {
        ltdContent.setCreateTime(DateUtils.getNowDate());
        return ltdContentMapper.insertLtdContent(ltdContent);
    }

    //存储图片

    public String addPic1(String uploadDir,String saveUrl,String str, MultipartFile file){
        try {
            // 构建文件路径
            String fileSavePath = saveUrl + str + file.getOriginalFilename();

            // 检查并创建目录
            File directory = new File(fileSavePath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            // 保存文件到本地
            File imageFile = new File(fileSavePath);
            file.transferTo(imageFile);

            return uploadDir + str + file.getOriginalFilename();
        } catch (IOException e) {
            e.printStackTrace();
            return str + file.getOriginalFilename();
        }
    }

    @Override
    public String addPic(String uploadDir,String saveUrl,String str,MultipartFile file)   {
        // 生成新文件名
        String originalFilename = file.getOriginalFilename();
        String newFilename = generateUniqueFilename(originalFilename);
        try {
            // 构建文件路径
            String fileSavePath = saveUrl + str;
            // 确保上传目录存在
            Path uploadPath = Paths.get(fileSavePath);
            if (!Files.exists(uploadPath)) {
                Files.createDirectories(uploadPath);
            }

            // 保存文件
            Path filePath = uploadPath.resolve(fileSavePath +newFilename);
            Files.copy(file.getInputStream(), filePath);
            return uploadDir + str + newFilename;
        }catch (IOException e) {
            e.printStackTrace();
            return str + newFilename;
        }

    }

    //新文件名
    public String generateUniqueFilename(String originalFilename) {
        // 生成 UUID（无连字符）
        String uuid = UUID.randomUUID().toString().replace("-", "");
        // 提取原始文件扩展名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 返回新文件名：UUID + 扩展名
        return uuid + extension;
    }
    /**
     * 新增内容 图片类型-->需存储图片
     *
     * @param ltdContent 内容
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult addLtdContentAndPic(LtdContent ltdContent, MultipartFile file) {
        String uploadUrl = ruoYiConfig.getPicAddress();
        String saveUrl = ruoYiConfig.getPicProfile();

        //存储图片
        addPic(null,saveUrl,ltdContent.getContentUrl(),file);

        //保存内容
        ltdContent.setCreateTime(DateUtils.getNowDate());
        ltdContent.setContentValue(uploadUrl + ltdContent.getContentUrl() + file.getOriginalFilename());
        return AjaxResult.success(ltdContentMapper.insertLtdContent(ltdContent));
    }

    /**
     * 新增内容
     *
     * @param ltdContent 内容
     * @return 结果
     */
    @Override
    public AjaxResult addLtdContent(LtdContent ltdContent) {
        ltdContent.setCreateTime(DateUtils.getNowDate());
        return AjaxResult.success(ltdContentMapper.insertLtdContent(ltdContent));
    }

    /**
     * 修改内容
     *
     * @param ltdContent 内容
     * @return 结果
     */
    @Override
    public int updateLtdContent(LtdContent ltdContent)
    {
        ltdContent.setUpdateTime(DateUtils.getNowDate());
        return ltdContentMapper.updateLtdContent(ltdContent);
    }

    /**
     * 修改内容 图片类型-->需修改图片
     *
     * @param ltdContent 内容
     * @return 结果
     */
    @Override
    public AjaxResult editLtdContentAndPic(LtdContent ltdContent, MultipartFile file) {
        String uploadUrl = ruoYiConfig.getPicAddress();
        String saveUrl = ruoYiConfig.getPicProfile();
        //存储图片
        addPic(null,saveUrl,ltdContent.getContentUrl(),file);

        //保存内容
        ltdContent.setUpdateTime(DateUtils.getNowDate());
        ltdContent.setContentValue(uploadUrl + ltdContent.getContentUrl() + file.getOriginalFilename());
        return AjaxResult.success(ltdContentMapper.updateLtdContent(ltdContent));
    }

    @Override
    public AjaxResult editLtdContent(LtdContent ltdContent) {
        ltdContent.setUpdateTime(DateUtils.getNowDate());
        return AjaxResult.success(ltdContentMapper.updateLtdContent(ltdContent));
    }

    /**
     * 批量删除内容
     *
     * @param contentIds 需要删除的内容主键
     * @return 结果
     */
    @Override
    public int deleteLtdContentByContentIds(Long[] contentIds)
    {
        return ltdContentMapper.deleteLtdContentByContentIds(contentIds);
    }

    /**
     * 删除内容信息
     *
     * @param contentId 内容主键
     * @return 结果
     */
    @Override
    public int deleteLtdContentByContentId(Long contentId)
    {
        return ltdContentMapper.deleteLtdContentByContentId(contentId);
    }
}
